﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Furnitures;
using blackhouse.BlackApart.Business;

namespace blackhouse.BlackApart.Web.Modules {
    public partial class Furnitures : System.Web.UI.UserControl {

        #region Fields

        private IFurniture[] furnitures;

        #endregion

        #region Properties

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.SetInstance();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.PopulateControls();
        }

        protected void rptFurnitures_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                IFurniture furniture = e.Item.DataItem as IFurniture;
                Image furnitureImage = e.Item.FindControl("imgFurniture") as Image;
                Label furnitureText = e.Item.FindControl("lblFurniture") as Label;
                if (furniture != null) {
                    if (furnitureImage != null)
                        ImageUtils.FurnitureImage(furnitureImage, furniture);
                    if (furnitureText != null)
                        furnitureText.Text = furniture.Name;
                }
            }
        }

        #endregion

        #region Methods

        private void SetInstance() {
            DisplayApartmentTypeState state = DisplayApartmentTypeState.GetCurrentState();
            if (state != null && state.ApartmentType != null) {
                this.furnitures = Factory.Instance().GetFurniture().GetApartmentTypeFurnitures(state.ApartmentType.Id);
            }
        }

        private void PopulateControls() {
            if (this.furnitures != null && this.furnitures.Length > 0) {
                this.rptFurnitures.DataSource = this.furnitures;
                this.rptFurnitures.DataBind();
            } else {
                this.pnlFurnitures.Visible = false;
            }
        }

        #endregion

    }
}